package com.njts.auto;
import org.springframework.beans.factory.annotation.Value;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.*;
import java.util.regex.Pattern;

public class AutoSearch {

    //这样多线程就不会出现问题了.

    public String getWhereComplementSql(Object object) throws InvocationTargetException, NoSuchMethodException, IllegalAccessException {
        HashMap<String, String> fields= setFields(object);// 储存设置
        String whereComplementSql = setWhereComplementSql(fields);// 生成where 后面要筛选的sql语句
        System.out.println(whereComplementSql);
        return whereComplementSql;
    }

    public String getSetComplementSql(Object object) throws InvocationTargetException, NoSuchMethodException, IllegalAccessException {
        HashMap<String, String> fields = setFields(object);// 储存设置
        String setComplementSql = setSetComplementSql(fields);// 生成where 后面要筛选的sql语句
        System.out.println(setComplementSql);
        return setComplementSql;
    }
    public String getValuesComplementSql(Object object) throws InvocationTargetException, NoSuchMethodException, IllegalAccessException {
        HashMap<String,String> fields=setFields(object);
        String valuesComplementSql= setValuesComplementSql(fields);
        System.out.println(valuesComplementSql);
        return valuesComplementSql;
    }
    /** 生成where 后面要筛选的sql语句
     * @param fields  存放着对象相关信息。
     * <ul>
     *      <li> key 为属性</li>
     *      <li> value 为属性值</li>
     * </ul>
     *  -key 里面有对象的属性和className<br>
     *  -value 里面有对象的属性值和类名(对象属于的类的类名)<br>
     *  -key 和 value 中属性和属性值一一对应。className 和 类名对应<br>
     *图示：<br>
     *      <ul>
     *         <li>
     *             表名.字段 等价 类名.属性
     *         </li>
     *      </ul>
     */
    public String setWhereComplementSql(HashMap<String,String> fields) {
        String whereComplementSql=" 1+1 "; //初始化生成Sql语句
        for(Object key: fields.keySet() ){  //提取sql语句中的key和value值
            String value= fields.get(key);
            if(!key.equals("className")) {  //筛选出hashMap中里面的key不是className的
                if (!value.equals("''")&&!value.equals("-1")&&!value.equals("'null'")&&!value.equals("0")&&!value.equals("0.0")&&!value.equals("'0'")) {
                    value=value.replaceAll("select|update|insert|delete|create",""); //防止sql注入,安全检查
                    if(key.equals("created")) { //当key等于created 做范围查询
                        whereComplementSql += " " + "and" + " " + fields.get("className") + "." + key + ">=" + value + " ";
                    }else if(key.equals("modified")){ //当key等于modified 做范围查询
                        whereComplementSql += " " + "and" + " " + fields.get("className") + "." + key + "<=" + value + " ";
                    }else{  //当key不等于created和modified 做等值查询
                        whereComplementSql += " " + "and" + " " + fields.get("className") + "." + key + "=" + value + " ";
                    }
                }
            }
        }
        return whereComplementSql;
    }
    public String setSetComplementSql(HashMap<String,String> fields) {
        boolean flag=true;
        String setComplementSql="";
        for(Object key: fields.keySet() ){  //提取sql语句中的key和value值
            String value= fields.get(key);
            if(!key.equals("className")) {  //筛选出hashMap中里面的key不是className的
                if (!value.equals("''") && !value.equals("0") && !value.equals("-1") && !value.equals("'null'")&&!value.equals("0.0") &&!value.equals("'0'")) {
                    value=value.replaceAll("select|update|insert|delete|create",""); //防止sql注入,安全检查
                    if (flag) {
                        flag=false;
                        setComplementSql += " " + fields.get("className") + "." + key + "=" + value + " ";
                    }else{
                        setComplementSql += " " + "," + " " + fields.get("className") + "." + key + "=" + value + " ";

                    }
                }
            }
        }
        return setComplementSql;
    }
    public String setValuesComplementSql(HashMap<String,String> fields){
        boolean flag=true;
        String valuesComplementSql="";
        String valuesComplementSql1="";
        String valuesComplementSql2="";
        for(Object key: fields.keySet() ){  //提取sql语句中的key和value值
            String value= fields.get(key);
            if(!key.equals("className")) {  //筛选出hashMap中里面的key不是className的

                if (!value.equals("''") && !value.equals("0") && !value.equals("-1") && !value.equals("'null'")&&!value.equals("0.0")&&!value.equals("'0'")) {
                    value=value.replaceAll("select|update|insert|delete|create",""); //防止sql注入,安全检查
                    if (flag) {
                        flag=false;
                        valuesComplementSql1 += " " + fields.get("className") + "(" + key ;
                        valuesComplementSql2 += "(" +value;
                    }else{
                        valuesComplementSql1 += "," +key+" ";
                        valuesComplementSql2 += ","+value;
                    }
                }
            }
        }
        valuesComplementSql= valuesComplementSql1+") values"+valuesComplementSql2+")";

        return valuesComplementSql;
    }


    /**设置对象相关信息的容器
     *
     * @param object 类
     * @throws NoSuchMethodException
     * @throws InvocationTargetException
     * @throws IllegalAccessException
     *
     *
     *
     */
    public HashMap<String,String> setFields(Object object) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
        HashMap<String,String> fields=new HashMap<>();
        Field[] declaredFields = object.getClass().getDeclaredFields(); //反射出对象的属性
        for(Field field : declaredFields){ // 枚举每个属性
            Class<?> type = field.getType();  // 获得属性的类型
            String fieldName=field.getName(); // 得到属性的名称
            String firstLetter=fieldName.substring(0,1).toUpperCase(); // 属性名首字母大写
            String Letter=fieldName.substring(1); // 提取非首字母的字母
            Method method = object.getClass().getMethod("get" + firstLetter + Letter); // 反射指定名称的方法
            Object value=method.invoke(object); // 执行接口获得其值
            String fieldValue;
            if(type.getName().equals("java.lang.String")) { // 属性的类型为String加上''。
                fieldValue = "'" + value + "'";
            }else{ // 其余不加''
                fieldValue =String.valueOf(value);
            }
            fields.put(fieldName,fieldValue); // 储存属性名和属性值
        }
        String className=object.getClass().getName(); // 获得类名
        className= className.replaceAll(".*\\.","").toLowerCase(); // 提取类名格式化为小写字符串
        fields.put("className",className); // 储存className和类名
        return fields;

    }
}

